package com.zjsru.oneday202208;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: likew
 * @Date: 2022/8/6
 *
 * 数组中的字符串匹配
 *
 * 输入：words = ["mass","as","hero","superhero"]
 * 输出：["as","hero"]
 * 解释："as" 是 "mass" 的子字符串，"hero" 是 "superhero" 的子字符串。
 * ["hero","as"] 也是有效的答案。
 */
public class StringMatching {
    public List<String> stringMatching(String[] words) {
        List<String> ansList = new ArrayList<>();
        int len = words.length;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                if(i == j){
                    continue;
                }
                if(words[i].contains(words[j]) && !ansList.contains(words[j])){
                    ansList.add(words[j]);
                }
            }
        }
        return ansList;
    }
    
    public static void main(String[] args) {
        StringMatching stringMatching = new StringMatching();
        String[] words = new String[]{"leetcoder","leetcode","od","hamlet","am"};
        System.out.println(stringMatching.stringMatching(words));
    }
}
